package dao;

import entity.Function;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import java.sql.*;
import java.util.List;

/**
 * Created by asus on 2017/6/17.
 */
@Repository
public class FunctionDAO extends BaseDAO {

    /**
     * Function的RowMapper映射类
     */
    private class FunctionRowMapper implements RowMapper<Function>{
        @Override
        public Function mapRow(ResultSet resultSet, int i) throws SQLException {
            Function function=new Function();
            function.setId(resultSet.getLong("id"));
            function.setName(resultSet.getString("name"));
            function.setAccordion(resultSet.getInt("accordion"));
            function.setParentId(resultSet.getLong("parent_id"));
            function.setSeriaNum(resultSet.getInt("serial_num"));
            function.setUrl(resultSet.getString("url"));
            return function;
        }
    }

    /**
     * 保存功能，目的可以将主键带出来
     * @param function
     */
    public void save(final Function function){
        final String sql="insert into auth_function(name,parent_id,url,serial_num,accordion) values(?,?,?,?,?)";
        KeyHolder keyHolder=new GeneratedKeyHolder();
        jdbcTemplate.update(new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                PreparedStatement preparedStatement=connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
                preparedStatement.setString(1,function.getName());
                preparedStatement.setLong(2,function.getParentId());
                preparedStatement.setString(3,function.getUrl());
                preparedStatement.setInt(4,function.getSeriaNum());
                preparedStatement.setInt(5,function.getAccordion());
                return preparedStatement;
            }
        },keyHolder);
        function.setId(keyHolder.getKey().longValue());
    }

    /**
     * 根据id更新url
     * @param id
     * @param url
     */
    public void updateUrl(Long id,String url){
        String sql="update from auth_function set url=? where id=?";
        jdbcTemplate.update(sql,url,id);
    }

    /**
     * 根据parentId去查找的分页功能
     * @param page
     * @param size
     * @param parentId
     * @return
     */
    public List<Function> findFunctions(int page,int size,Long parentId){
        String sql="select * from auth_function where parent_id=? limit ?,?";
        try{
            return jdbcTemplate.query(sql,new Object[]{parentId,(page-1)*size,size},new FunctionRowMapper());
        }catch (Exception e){
            //logger
            return null;
        }
    }

    /**
     * 根据id删除function
     * @param id
     */
    public void deleteById(Long id){
        String sql="delete from auth_function where id=?";
        jdbcTemplate.update(sql,id);
    }

    /**
     * 找出所有的function
     * @return
     */
    public List<Function> findAllFunction(){
        String sql="select * from auth_function";
        try{
            return jdbcTemplate.query(sql,new FunctionRowMapper());
        }catch (Exception e){
            //logger
            return null;
        }
    }
}
